package com.mrjy.queries.bo;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.dto.MyMessagesDto;
import com.mrjy.common.entity.HappinessInfo;
import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.QuestionGoodInfo;
import com.mrjy.common.entity.QuestionInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.integral.dao.IHappinessInfoDao;
import com.mrjy.integral.util.HappinessInfoEnum;
import com.mrjy.integral.util.HappinessInfoUtil;
import com.mrjy.integral.util.IntegralEnum;
import com.mrjy.integral.util.IntegralUtil;
import com.mrjy.queries.dao.IQuestionCommentDao;
import com.mrjy.queries.dao.IQuestionGoodInfoDao;
import com.mrjy.queries.dao.IQuestionInfoDao;
import com.mrjy.queries.dto.QuestionGoodInfoDTO;
import com.mrjy.queries.util.MyMessagesDtoComparator;

@Service("QuestionGoodInfoBo")
public class QuestionGoodInfoBo implements IQuestionGoodInfoBo{

	@Resource
	private IQuestionInfoDao questionInfoDao;
	@Resource
	private IQuestionGoodInfoDao questionGoodInfoDao;
	@Resource
	private IQuestionCommentDao questionCommentDao;
	@Resource
	private IHappinessInfoDao happinessInfoDao;
	
	/**
	 * 我的问题 点赞
	 */
	@Override
	public String addGood(QuestionGoodInfo goodInfo, UserInfo userInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		QuestionInfo question = new QuestionInfo();
		if (goodInfo.getQuestionId()==null) {
			return "error";
		}
		question.setId(goodInfo.getQuestionId());
		question.setDisableFlag(0);
		QuestionInfo existQ = questionInfoDao.queryById(question);
		if (existQ != null) {
			goodInfo.setUserId(userInfo.getId());
			QuestionGoodInfo existGood = questionGoodInfoDao.queryQuestionGood(goodInfo);
			if (existGood == null) {
				goodInfo.setGoodTime(new Date());
				questionGoodInfoDao.insert(goodInfo);
				//回写问答信息表questionInfo的点赞数goodNum
				questionInfoDao.addGoodNum(existQ.getId());
				return "ok";
			}else {
				return "error";
			}
			
		}
		return "error";
	}
	
	
	@Override
	public Object txInsertGood(QuestionGoodInfo info,
			Map<String, Object> map) {
		if(null == info){
			map.put("err_code", -2);
			map.put("err_msg", "点赞内容不能为空");
			return map;
		}
		info.setGoodTime(new Date());
		info.setReadFlag(0);
		
		QuestionGoodInfo existGoodInfo =  questionGoodInfoDao.queryExist(info);
		if(null != existGoodInfo){
			map.put("err_code", 1);
			map.put("err_msg", "已经点过赞");
			return map;
		}else{
			QuestionComment comment = new QuestionComment();
			comment.setId(info.getCommentId());
			QuestionComment questionComment = questionCommentDao.queryById(comment);
			info.setBeGoodUserId(questionComment.getUserId());//被点赞人的id
			int result = questionGoodInfoDao.insert(info);
			if(result == 1){
				if (info.getQuestionId() != null) {
					questionInfoDao.addGoodNum(info.getQuestionId());
				}
				if (info.getCommentId() != null) {
					questionCommentDao.addGoodNum(info.getCommentId());
				}
				//添加积分
				IntegralInfo integral = IntegralUtil.setIntegral(info.getUserId()
        				, IntegralEnum.GOOD.getScore()
        				, IntegralEnum.GOOD.getTypeDesc()
        				, IntegralEnum.GOOD.getTypeId()
        				, info.getId());
				
				//爱值添加
				HappinessInfo hi = HappinessInfoUtil.setHappinessInfo(info.getUserId()
						, HappinessInfoEnum.GOOD.getLoveValue(), HappinessInfoEnum.GOOD.getTypeDesc()
						, HappinessInfoEnum.GOOD.getTypeId(), "0", info.getId());
				happinessInfoDao.insert(hi);
				
    			map.put("err_code", 0);
				map.put("err_msg", "点赞成功");
                map.put("score", integral.getScore());

            }else{
				map.put("err_code", 2);
				map.put("err_msg", "点赞失败");
			}
		}
		
		return map;
	}


	@Override
	public Object quesyExist(QuestionGoodInfo info, Map<String, Object> map) {
		if(null == info){
			map.put("err_code", -2);
			map.put("err_msg", "传入对象内容不存在");
			return map;
		}
		
		if(info.getQuestionId() !=null || info.getCommentId() != null){
			QuestionGoodInfo existGoodInfo =  questionGoodInfoDao.queryExist(info);
			if(null != existGoodInfo){
				map.put("err_code", 0);
				map.put("err_msg", "已经点过赞");
				map.put("info", existGoodInfo);
			}else{
				map.put("err_code", 1);
				map.put("err_msg", "未点过赞");
				map.put("info", existGoodInfo);
			}
		}
		return map;
	}


	@Override
	public Object txUpdateReadFlagToReaded(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = questionGoodInfoDao.updateReadFlagToReaded(id);
		if (count > 0) {
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败");
		}
		return map;
	}


	@Override
	public Object queryMyGoodPageByCommentedId(MyMessagesDto dto) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		List<MyMessagesDto> listGoodnum = questionCommentDao.queryMyMessagesGoodNumPageByCommentedId(dto);
		MyMessagesDtoComparator comparator = new MyMessagesDtoComparator();
        Collections.sort(listGoodnum, comparator);
		if(listGoodnum!=null){
			//查询用户的点赞信息
		    map.put("err_code",0);
		    map.put("err_msg", "查询有数据");
		    map.put("rows",listGoodnum);
		 }else{
			 map.put("err_code",1);
		     map.put("err_msg", "查询无数据");
		 }
		return map;
	}


	@Override
	public Object queryMyPraise(QuestionGoodInfoDTO dto) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		List<QuestionGoodInfoDTO> list = questionGoodInfoDao.queryMyPraise();
		if(list.size()>0){
			map.put("err_code", 0);
			map.put("err_msg", "查询有结果");
			map.put("list", list);
			
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询无结果");
			map.put("list", null);
		}
		//当前用户的排名信息
		QuestionGoodInfoDTO myDTO = questionGoodInfoDao.queryMyPraiseById(dto.getUserId());
		map.put("rows", myDTO);
		
		return map;
	}

}
